[contents] [prev] [next] [top] [bottom] (14 out of 17)

Arrays and Keyed Linked Lists

The ScriptX language provides two literal constructs that directly represent collections of objects in scripts: an array literal and a keyed linked list literal. In addition, ScriptX provides two simple language constructs for accessing members and testing for membership in collections of all kinds.

Array and KeyedLinkedList are two of the many subclasses of Collection that can be used in a ScriptX program. Collections provide comprehensive behavior for groupings of objects. Array and KeyedLinkedList are prime representations of two categories of collection: those with explicit keys and those without. Collections are described in detail in Chapter 7, "Collections," and in the "Collections" chapter of ScriptX Components Guide.

Array Literal

The array literal construct contains any number of expressions, separated by commas, surrounded by parentheses, and beginning with a hash sign ( # ):

#(expression, expression, expression, expression, . . . )
When the array construct is evaluated, each individual expression is also evaluated, in order, so that the resulting array contains the result of each of those expressions. Arrays themselves are instances of the class Array. Here are some examples of array literals:

#(2, 4.2 + 5, "Fresno", 3 < 4, #(@doug, @jocelyn, @jim))
#(2, 9.19999694824219, "Fresno", true, #(@doug, @jocelyn, @jim))
#() -- empty array
#()

In some languages, all items in an array must be of the same type. The first example demonstrates that in ScriptX, members of an array can belong to different classes. The five members of this array belong to the classes ImmediateInteger, ImmediateFloat, StringConstant, Boolean, and Array, respectively.

Keyed Linked List Literal

The keyed linked list literal is similar to the array literal, except that the expressions within the parentheses are unordered key-value pairs. The keyed linked list construct evaluates to an instance of KeyedLinkedList.

#(key:value, key:value, key:value, . . . )
In the keyed linked list literal, key-value pairs are separated by commas, with colons separating the key from the value. Like the array construct, all the expressions within the keyed list literal are evaluated before the instance of the KeyedLinkedList is created.

The value part of a key-value pair can be any expression. To specify most complex expressions as the key part of the pair, you must specify that expression inside parentheses (and the result of that expression becomes the key itself). The following expressions can be used without parentheses as the key part of the key-value pair:

Here are several examples of the use of keyed linked list literals:

#(@fruit: "apple", @vegetable: "carrot", @legume: "kidney bean")
#(:) -- empty keyed list
#(1:"money", 2:"show", 3:"ready", 4:"go")
#( #(1,2,3):@oneToThree, #(4,5,6):@fourToSix, \
#(7,8,9,10):@sevenToTen) )

x := 1; y := 4
#( (x < y):@smallX, (x > y):@smallY )
#(true:@smallX, false:@smallY)

Literals and Other Collections

You can use the array and keyed list literals, together with coercion, to represent any kind of collection "literally" in a script.

#("Bornstein","Agostino","Reiman","Jacobi") as SortedArray
#("Agostino", "Bornstein", "Jacobi", "Reiman") as SortedArray

Coercion is discussed in the section "Coercing Objects to Other Classes" on page 66, and again in the section "Coercing Between Collection Classes" on page 156.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.